<template>
  <div>
    <el-dialog
      title="备注详情"
      :visible.sync="show"
      width="40rem"
      :before-close="closeDialog"
    >
      <!-- 表单信息 -->
      <el-form label-position="left" label-width="80px" :model="remark">
        <el-form-item label="备注内容">
          <el-input v-model="remark.remarkInfo" maxlength="30" show-word-limit />
        </el-form-item>
      </el-form>
      <!-- 确定和取消 -->
      <span slot="footer" class="dialog-footer">
        <el-button v-if="type===2" type="danger" @click="deleteRemark()">删除</el-button>
        <el-button type="primary" @click="sendDialog()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { addRemark, deleteRemark, updateRemark, getRemarkBysendId } from '../../api/remark.js'
export default {
  data() {
    return {
      // 页面存储数据
      show: false,
      remark: {},
      type: 1 // 1添加,2修改
    }
  },
  created() {
    // dialog信息初始化
    this.createDialogStatus
  },
  methods: {
    // dialog信息初始化
    createDialogStatus() {
      this.show = false
    },
    // 展示dialog弹出框
    showDialog(data) {
      // data.userId -> 当前用户编号 data.sendId -> 当前send表编号（如果没有为none）
      this.$set(this.remark, 'sendId', data.sendId)
      if (data.sendId === 'none') {
        this.$notify({
          title: '请先进行评分或审核再进行备注',
          type: 'error'
        })
      } else {
        getRemarkBysendId(this.remark).then(data => {
          if (data.obj.length === 0) {
            this.type = 1
          } else {
            this.type = 2
            this.$set(this.remark, 'remarkId', data.obj[0].remarkId)
            this.$set(this.remark, 'remarkInfo', data.obj[0].remarkInfo)
          }
        }).then(data => {
          this.show = true
        })
      }
    },
    // 提交dialog信息
    sendDialog() {
      if (this.type === 1) {
        addRemark(this.remark).then(data => {
          this.show = false
          this.remark = {}
          if (data.result === false) {
            this.$notify({
              title: '添加失败',
              type: 'error'
            })
          } else if (data.result === true) {
            this.$notify({
              title: '添加成功',
              type: 'success'
            })
          }
        })
      } else if (this.type === 2) {
        updateRemark(this.remark).then(data => {
          this.show = false
          this.remark = {}
          if (data.result === false) {
            this.$notify({
              title: '修改失败',
              type: 'error'
            })
          } else if (data.result === true) {
            this.$notify({
              title: '修改成功',
              type: 'success'
            })
          }
        })
      }
    },
    // 删除备注
    deleteRemark() {
      this.$confirm('是否确定删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRemark(this.remark).then(data => {
          this.show = false
          this.remark = {}
          if (data.result === false) {
            this.$notify({
              title: '删除失败',
              type: 'error'
            })
          } else if (data.result === true) {
            this.$notify({
              title: '删除成功',
              type: 'success'
            })
          }
        })
      }).catch(() => {})
    },
    // 关闭dialog
    closeDialog() {
      this.$confirm('是否确定取消', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.show = false
        this.remark = {}
      }).catch(() => {})
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
